#!/usr/bin/env python3
"""测试关系类型检测功能"""

import asyncio
from database_analyzer import DatabaseAnalyzer
from models import DatabaseType, DatabaseConnection

async def test_relationships():
    """测试关系类型检测"""
    
    # SQLite连接信息
    connection = DatabaseConnection(
        db_type=DatabaseType.SQLITE,
        host="example_ecommerce.db",
        port=0,
        database="example_ecommerce.db",
        username="",
        password=""
    )
    
    # 创建分析器
    analyzer = DatabaseAnalyzer(connection)
    
    # 获取关系
    relationships = analyzer.get_relationships()
    
    print(f"找到 {len(relationships)} 个关系:")
    print("-" * 80)
    
    for rel in relationships:
        print(f"表: {rel.from_table}.{rel.from_column} -> {rel.to_table}.{rel.to_column}")
        print(f"  关系类型: {rel.relationship_type}")
        print(f"  基数: {rel.cardinality}")
        print(f"  唯一约束: {rel.is_unique}")
        print(f"  多对多: {rel.is_many_to_many}")
        print(f"  描述: {rel.description}")
        print()
    
    # 验证一些预期关系
    print("验证特定关系:")
    print("-" * 40)
    
    # 用户-订单关系应该是1:N
    user_order_rels = [r for r in relationships 
                      if r.from_table == "orders" and r.to_table == "users"]
    if user_order_rels:
        rel = user_order_rels[0]
        print(f"用户-订单关系: {rel.cardinality} (预期: 1:N)")
    
    # 产品-分类关系应该是N:1
    product_category_rels = [r for r in relationships 
                           if r.from_table == "products" and r.to_table == "categories"]
    if product_category_rels:
        rel = product_category_rels[0]
        print(f"产品-分类关系: {rel.cardinality} (预期: N:1)")

if __name__ == "__main__":
    asyncio.run(test_relationships())